package recorder.input;

import java.util.ArrayList;

import core.input.Input;

public class InputRecorder implements Input{
	
	private ArrayList<Boolean> keyPressed = new ArrayList<Boolean>();
	private ArrayList<Boolean> mouseDown = new ArrayList<Boolean>();
	private int mouseX = -1;
	private int mouseY = -1;
	
	@Override
	public synchronized boolean isKeyPressed(int code){
		if(code >= keyPressed.size()){
			increaseSize(keyPressed, code+1);
		}
		return keyPressed.get(code);
	}
	
	public synchronized void setKeyPressed(int code, boolean value){
		if(code >= keyPressed.size()){
			increaseSize(keyPressed, code+1);
		}
		keyPressed.set(code, value);
	}

	@Override
	public synchronized boolean isMouseDown(int code){
		if(code >= mouseDown.size()){
			increaseSize(mouseDown, code+1);
		}
		return mouseDown.get(code);
	}
	
	public synchronized void setMouseDown(int code, boolean value){
		if(code >= mouseDown.size()){
			increaseSize(mouseDown, code+1);
		}
		mouseDown.set(code, value);
	}
	
	@Override
	public synchronized int getMouseX(){
		return mouseX;
	}
	
	@Override
	public synchronized int getMouseY(){
		return mouseY;
	}
	
	public synchronized void setMouseX(int pos){
		mouseX = pos;
	}
	
	public synchronized void setMouseY(int pos){
		mouseY = pos;
	}
	
	public synchronized void reset(){
		mouseX = -1;
		mouseY = -1;
		for(int i=0; i<keyPressed.size(); i++){
			keyPressed.set(i, false);
		}
		for(int i=0; i<mouseDown.size(); i++){
			mouseDown.set(i, false);
		}
	}
	
	
	//****************************** HELPING METHODS ***************************
	private void increaseSize(ArrayList<Boolean> list, int newSize) {
		while(list.size() < newSize){
			list.add(false);
		}
	}

}
